package com.heima.search.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.search.mapper.SearchMapper;


import com.heima.search.pojo.Item;
import com.heima.search.pojo.RequestParams;
import com.heima.search.service.SearchService;
import com.hmall.common.dto.ItemDoc;
import com.hmall.common.result.PageResult;
import com.hmall.feign.clients.ItemFeignClient;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class SearchServiceImpl extends ServiceImpl<SearchMapper, Item> implements SearchService {
    @Autowired
    private ItemFeignClient itemFeignClient;
    @Autowired
    private SearchMapper searchMapper;
    @Autowired
    private ThreadPoolExecutor myThreadPool;
    @Autowired
    private RestHighLevelClient client;

    /**
     * 将db数据批量导入es中 使用feign和多线程操作
     */
    @Override
    public void dbToEs() {
//        计算用时
        long startTime = System.currentTimeMillis();
//        1.获取总条数？
//        Integer allCount = searchMapper.selectCount(null);
//             todo 导入es的数据 只要求item是上架的 status=1的，并且 分页时 也要控制 条件
        Integer allCount = searchMapper.selectCount(new LambdaQueryWrapper<Item>().eq(Item::getStatus, 1));
//        2.设置多少条数据为一个批次
        Integer pageSize = 50;
//        2.1一共处理的批次数
        Integer allPage = allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1;
//        3.循环处理 调用多线程，创建线程池()从第一页开始
//        3.1等待分线程的执行函数
        CountDownLatch cdl = new CountDownLatch(allPage);
        for (Integer i = 1; i <= allPage; i++) {
            myThreadPool.submit(new ThreadTask(i, pageSize, cdl));
        }
//        4.使用countDownLatch等主线程
        try {
            cdl.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        5.结束时间
        long endTime = System.currentTimeMillis();
        System.out.println("db导入es耗时：" + (endTime - startTime) + "毫秒！");

    }


    class ThreadTask implements Runnable {
        int i;
        int pageSize;
        CountDownLatch cdl;

        public ThreadTask(Integer i, Integer pageSize, CountDownLatch cdl) {
            this.i = i;
            this.pageSize = pageSize;
            this.cdl = cdl;
        }

        @Override
        public void run() {
//            2.使用feign调用page，获取数据
            PageResult<com.hmall.common.dto.Item> page = itemFeignClient.pageES(i, pageSize);
            List<com.hmall.common.dto.Item> list = page.getList();
//            构建批处理对象
            BulkRequest bulkRequest = new BulkRequest();
            for (com.hmall.common.dto.Item item : list) {
//            3.将db转成doc
                ItemDoc itemDoc = new ItemDoc();
//                使用属性拷贝
                BeanUtils.copyProperties(item, itemDoc);
//                设置suggestion搜索中自动补齐
                String brand = item.getBrand();
                String category = item.getCategory();
                String name = item.getName();
//                按照空格分隔name
                if (name.contains(" ")) {
                    String[] strings = name.split(" ");
                    List<String> listSuggestion = new ArrayList<>();
                    listSuggestion.add(category);
                    listSuggestion.add(brand);
                    Collections.addAll(listSuggestion, strings);
                    itemDoc.setSuggestion(listSuggestion);
                } else {
                    itemDoc.setSuggestion(Arrays.asList(name, category, brand));
                }
//                4.添加到批处理中，将doc转成json对象
//                todo 注意此处的索引名称大小写问题？？？
                bulkRequest.add(new IndexRequest("items")
                        .id(itemDoc.getId().toString())
                        .source(JSON.toJSONString(itemDoc), XContentType.JSON));
            }
//            5.一次发送数据
            try {
                client.bulk(bulkRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
//            每次执行就将cdl值减一
            cdl.countDown();
        }
    }

    /**
     * 部分数据导入测试
     */
    @Override
    public void pageTest() {
        PageResult<com.hmall.common.dto.Item> list = itemFeignClient.pageES(1, 11);
        List<com.hmall.common.dto.Item> items = list.getList();
        BulkRequest bulkRequest = new BulkRequest();
        for (com.hmall.common.dto.Item item : items) {
            ItemDoc itemDoc = new ItemDoc();
//                使用属性拷贝
            BeanUtils.copyProperties(item, itemDoc);
//                4.添加到批处理中，将doc转成json对象
//                todo 注意此处的索引名称大小写问题？？？
            bulkRequest.add(new IndexRequest("items")
                    .id(itemDoc.getId().toString())
                    .source(JSON.toJSONString(itemDoc), XContentType.JSON));
        }
//            5.一次发送数据
        try {
            client.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void noPool() {
        long startTime = System.currentTimeMillis();
//        1.获取总条数？
        Integer allCount = searchMapper.selectCount(new LambdaQueryWrapper<Item>().eq(Item::getStatus, 1));
//        2.设置多少条数据为一个批次
        Integer pageSize = 50;
//        2.1一共处理的批次数
        Integer allPage = allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1;


        for (Integer i = 1; i <= allPage; i++) {
            PageResult<com.hmall.common.dto.Item> page = itemFeignClient.pageES(i, pageSize);
            List<com.hmall.common.dto.Item> list = page.getList();
//            构建批处理对象
            BulkRequest bulkRequest = new BulkRequest();
            for (com.hmall.common.dto.Item item : list) {
//            3.将db转成doc
                ItemDoc itemDoc = new ItemDoc();
//                使用属性拷贝
                BeanUtils.copyProperties(item, itemDoc);
//                4.添加到批处理中，将doc转成json对象
//                todo 注意此处的索引名称大小写问题？？？
                bulkRequest.add(new IndexRequest("items")
                        .id(itemDoc.getId().toString())
                        .source(JSON.toJSONString(itemDoc), XContentType.JSON));
            }
//            5.一次发送数据
            try {
                client.bulk(bulkRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            long endTime = System.currentTimeMillis();
            System.out.println("db导入es耗时：" + (endTime - startTime) + "毫秒！");
        }
    }
//===================================es业务处理==========================================================

    /**
     * items的 分页 搜索 排序 过滤等
     *
     * @param params
     * @return
     */
    private final static String SEARCH_INDEX = "items";

    @Override
    public PageResult listItems(RequestParams params) {
//        1.创建请求对象
        SearchRequest searchRequest = new SearchRequest(SEARCH_INDEX);
//        2.构建DSL语句（抽到一个方法中）
        buildersDSL(params, searchRequest);
//        3.分页
//        3.1获取分页参数
        Integer page = params.getPage();
        Integer size = params.getSize();
//        分页要在所有查询中（前面的构建DSL布尔语句时 已经查询全部了 此处再查询就会出错了.+）
//        searchRequest.source().query(QueryBuilders.matchAllQuery()).from((page - 1) * size).size(size)
        searchRequest.source().from((page - 1) * size).size(size);
//        4.todo 排序？(会影响算分函数的结果 导致会先按照排序的操作 然后再按照算分函数结果)

//                .sort("sold", SortOrder.DESC)
//                .sort("price", SortOrder.ASC);
//        5.发送请求
        SearchResponse response = null;
        try {
            response = client.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
//        6.解析结果 抽到一个方法中\
//        if (response != null) {
//            com.heima.search.result.PageResult result = resultHandle(response);
//        }
        return resultHandle(response);
    }


    /**
     * 响应结果的解析
     *
     * @param response
     * @return
     */
    private PageResult resultHandle(SearchResponse response) {
//        1.构建分页结果返回
        PageResult<ItemDoc> pageResult = new PageResult<>();
//        2.获取总条数
        SearchHits responseHits = response.getHits();
        long value = responseHits.getTotalHits().value;
        pageResult.setTotal(value);
//        3.获取hits
        SearchHit[] hits = responseHits.getHits();
//        4.解析每一个hit 并 转换成JSON对象
//        4.1 创建list集合
        List<ItemDoc> itemDocs = new ArrayList<>();
        for (SearchHit hit : hits) {
//            1.获取文本
            String sourceAsString = hit.getSourceAsString();
            ItemDoc itemDoc = JSON.parseObject(sourceAsString, ItemDoc.class);

//        5.解析高亮
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
//            确保获取到了高亮的结果查询
            if (!CollectionUtils.isEmpty(highlightFields)) {
//            5.1获取高亮部分
                HighlightField name = highlightFields.get("name");
//                判断name高亮字段是否为非空
                if (name != null) {
                    String highLightName = name.getFragments()[0].toString();
//            5.2 设置高亮子字段
                    itemDoc.setName(highLightName);
                }
            }
            //            收集所有doc
            itemDocs.add(itemDoc);
        }
//        6.返回分页结果
        pageResult.setList(itemDocs);
        return pageResult;
    }

    /**
     * 构建DSL的语句 工具 包含 关键字的搜索 过滤 高亮 算分函数
     *
     * @param params
     * @param searchRequest
     */
    private void buildersDSL(RequestParams params, SearchRequest searchRequest) {
//        构建一个sourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        1.判断有无key 走 高亮或者全文检索（算分must）
//        创建布尔builder
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        String key = params.getKey();
        if (key != null && !"".equals(key)) {
//          存在key 关键字搜索
            boolQueryBuilder.must(QueryBuilders.matchQuery("all", key));
//            创建高亮
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            highlightBuilder.field("name").requireFieldMatch(false);
            searchSourceBuilder.highlighter(highlightBuilder);
        } else {
//            走全文检索
            boolQueryBuilder.must(QueryBuilders.matchAllQuery());
        }
//        2.布尔组合条件过滤
//        2.1分类 Category
        String category = params.getCategory();
        if (category != null && !"".equals(category)) {
            boolQueryBuilder.filter(QueryBuilders.termQuery("category", category));
        }
//        2.2品牌 brand
        String brand = params.getBrand();
        if (brand != null && !"".equals(brand)) {
            boolQueryBuilder.filter(QueryBuilders.termQuery("brand", brand));
        }
//        2.3价格 price
        Integer maxPrice = params.getMaxPrice();
        Integer minPrice = params.getMinPrice();
        if (maxPrice != null && minPrice != null) {
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(minPrice).lt(maxPrice));
        }
//        2.4 将布尔查询 放入searchSourceBuilder中
        searchSourceBuilder.query(boolQueryBuilder);
//        3.算分函数（商品竞价票排名）
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(
                boolQueryBuilder,//需要参与算分的
                new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{
                        new FunctionScoreQueryBuilder.FilterFunctionBuilder(
                                QueryBuilders.termQuery("isAD", true),
//                                算分函数 如何处理
                                ScoreFunctionBuilders.weightFactorFunction(100)
                        )
                }
        );
//        3.1将算分函数放入到searchSourceBuilder中
        searchSourceBuilder.query(functionScoreQueryBuilder);
//        4.将所有的searchSourceBuilder放入source中
        searchRequest.source(searchSourceBuilder);
    }

    /**
     * 桶的分类suggestion
     */
    @Override
    public Map<String, List<String>> filters(RequestParams params) {
        try {
//        1.创建请求
            SearchRequest searchRequest = new SearchRequest(SEARCH_INDEX);
//        2.构建查询和搜索一样构建DSL语句
            buildersDSL(params, searchRequest);
//        3.0设置返回数据大小
            searchRequest.source().size(0);
//        3.聚合
            aggregationDSL(searchRequest);
//        4.发送请求
            SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//        5.解析结果
            HashMap<String, List<String>> map = new HashMap<>();
            Aggregations aggregations = response.getAggregations();
//            分类
            List<String> categoryList = returnList(aggregations, "category_agg");
            map.put("category", categoryList);
//            品牌
            List<String> brandList = returnList(aggregations, "brand_agg");
            map.put("brand", brandList);
//        6.返回结果
            return map;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * items的搜索自动补全功能
     *
     * @param key
     * @return
     */
    @Override
    public List<String> suggestion(String key) {
        try {
//        1.准备请求
            SearchRequest searchRequest = new SearchRequest(SEARCH_INDEX);
//        2.准备DSL的suggestion语句
            String suggestName = "mySuggestion";
            searchRequest.source().suggest(new SuggestBuilder().addSuggestion(
                    suggestName,
                    SuggestBuilders
                            .completionSuggestion("suggestion")
                            .prefix(key)
                            .skipDuplicates(true)
                            .size(20)
            ));
//        3.发送请求
            SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//        4.解析请求
            Suggest suggest = response.getSuggest();
            List<String> sugList = new ArrayList<>();
            CompletionSuggestion suggestion = suggest.getSuggestion(suggestName);
            for (CompletionSuggestion.Entry.Option option : suggestion.getOptions()) {
                String string = option.getText().toString();
                sugList.add(string);
            }
            return sugList;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
//============异步调用mq执行对es的crud=========

    /**
     * 对DB的新增 修改的es操作
     *
     * @param id
     */
    @Override
    public void myMqItemListenerInsertOrUpdate(Long id) {
        try {
//        1.需要从db中查询数据
            Item item = searchMapper.selectById(id);
//        2.创建请求
            IndexRequest request = new IndexRequest(SEARCH_INDEX).id(item.getId().toString());
//        3.构建DSL语句
//      todo 非常关键的字段处理Suggestion  3.1将db转成doc
            ItemDoc itemDoc = new ItemDoc();
//                使用属性拷贝
            BeanUtils.copyProperties(item, itemDoc);
//                设置suggestion搜索中自动补齐
            String brand = item.getBrand();
            String category = item.getCategory();
            String name = item.getName();
//                按照空格分隔name
            if (name.contains(" ")) {
                String[] strings = name.split(" ");
                List<String> listSuggestion = new ArrayList<>();
                listSuggestion.add(category);
                listSuggestion.add(brand);
                Collections.addAll(listSuggestion, strings);
                itemDoc.setSuggestion(listSuggestion);
            } else {
                itemDoc.setSuggestion(Arrays.asList(name, category, brand));
            }
//        对象转JSON
            request.source(JSON.toJSONString(itemDoc), XContentType.JSON);
//        4.发送请求
            client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *删除es的数据 使用MQ的异步调用
     * @param id
     */
    @Override
    public void myMQItemListenerDelete(Long id) {
        try {
//        1.创建请求
            DeleteRequest deleteRequest = new DeleteRequest(SEARCH_INDEX, id.toString());
//        3.发送请求
            client.delete(deleteRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static List<String> returnList(Aggregations aggregations, String aggName) {
//        解析一定记住 使用什么聚合的类型 就用什么聚合的类型接收
        Terms aggregation = aggregations.get(aggName);
        List<? extends Terms.Bucket> buckets = aggregation.getBuckets();
        List<String> list = new ArrayList<>();
        for (Terms.Bucket bucket : buckets) {
            String keyAsString = bucket.getKeyAsString();
            list.add(keyAsString);
        }

        return list;
    }

    /**
     * 构建聚合语句
     *
     * @param searchRequest
     */
    private void aggregationDSL(SearchRequest searchRequest) {
//        1.分类
        searchRequest.source().aggregation(
                AggregationBuilders
//                        聚合的类型
                        .terms("category_agg")
                        .field("category")
                        .size(20)
        );
//        2.品牌
        searchRequest.source().aggregation(
                AggregationBuilders
                        .terms("brand_agg")
                        .field("brand")
                        .size(20)
        );
    }

}

